import {
    createStore
} from 'vuex'

import createPersistedState from "vuex-persistedstate";  //安装 cnpm install --save vuex-persistedstate

export default createStore({
    state: {
        /**
         * 存储登录数据
         */
        token: "",
        /**
         * 头部标签
         */
        tags: [],
    },
    mutations: {
        /**
         * 存储token
         */
        settoken(state, playout) {
            state.token = playout;
            // console.log(state.token, "2222");
        },
        /***
         * 存储头部标签
         */
        addTags(state, playout) {
            // 处理重复
            const isFind = state.tags.find(item => {
                return item.path === playout.path
            })
            if (!isFind) {
                state.tags.push(playout)
            }
        },
        /**
         * 删除tags
         */
        //  arr.splice(index,n,add)
        //  三个参数：
        //  index : 开始操作的位置，数组的下标
        //      n : 要删除的个数
        //    add : 要添加的元素（可以是多个）
         
        removeTagsView(state, payload) {
            // state.tags.splice(playout, 1)
            if (payload.type === 'index') {
                state.tags.splice(payload.index, 1)
                return
            } else if (payload.type === 'other') {
                state.tags.splice(
                    payload.index + 1,
                    state.tags.length - payload.index + 1
                )
                state.tags.splice(0, payload.index)
            } else if (payload.type === 'right') {
                state.tags.splice(
                    payload.index + 1,
                    state.tags.length - payload.index + 1
                )
            }
        }
    },
    getters: {
    },
    actions: {
        cleantoken({ commit }) {
            commit("settoken", "")
            localStorage.removeItem("rystore");
            location.href = "/"

        }
    },
    modules: {},
    plugins: [
        createPersistedState({
            storage: window.localStorage,
            key: "rystore",
            render(state) {
                return {
                    ...state
                };
            }
        })
    ]
})
